package com.ktkj.init.controller;

import com.ktkj.init.dto.LoginForm;
import com.ktkj.init.entity.UserInfo;
import com.ktkj.init.exception.BusinessException;
import com.ktkj.init.response.Response;
import com.ktkj.init.response.ResponseCallBack;
import com.ktkj.init.response.ResponseCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 登录控制器
 *
 * @author yanlixiong
 **/
@Api(tags = "登录管理")
@RestController
public class LoginController {

    @ApiOperation(value = "登录")
    @PostMapping(path = "/login")
    public Response login(@Valid @ModelAttribute final LoginForm form) {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                //账号密码令牌
                AuthenticationToken token = new UsernamePasswordToken(form.getUserName().trim(), form.getPassword().trim());
                //获得当前用户到登录对象，现在状态为未认证
                Subject subject = SecurityUtils.getSubject();
                try {
                    //将令牌传到shiro提供的login方法验证，需要自定义realm
                    subject.login(token);
                    //没有异常表示验证成功,进入首页
                    UserInfo userInfo = (UserInfo) subject.getPrincipal();
                    criteria.addSingleResult(userInfo);
                } catch (IncorrectCredentialsException ice) {
                    throw new BusinessException(BusinessException.BUSSINESS_EXCEPTION, "用户名或密码不正确！");
                } catch (UnknownAccountException uae) {
                    throw new BusinessException(BusinessException.BUSSINESS_EXCEPTION, "未知账户！");
                } catch (DisabledAccountException dae) {
                    throw new BusinessException(BusinessException.BUSSINESS_EXCEPTION, "账户被禁用！");
                } catch (ExcessiveAttemptsException eae) {
                    throw new BusinessException(BusinessException.BUSSINESS_EXCEPTION, "用户名或密码错误次数太多！");
                } catch (AuthenticationException ae) {
                    throw new BusinessException(BusinessException.BUSSINESS_EXCEPTION, "验证未通过！");
                } catch (Exception e) {
                    throw new BusinessException(BusinessException.BUSSINESS_EXCEPTION, "验证未通过！");
                }
            }
        }.sendRequest();
    }


    @ApiOperation(value = "注销")
    @PostMapping(path = "/logout")
    public Response saveOrUpdate() {
        return new ResponseCallBack() {
            @Override
            public void execute(ResponseCriteria criteria, Object... obj) {
                Subject subject = SecurityUtils.getSubject();
                subject.logout();
                criteria.addSingleResult(true);
            }
        }.sendRequest();
    }
}
